package com.oracle.mall.controller;

import com.alibaba.fastjson.JSON;

import com.oracle.mall.po.*;
import com.oracle.mall.service.CartService;
import com.oracle.mall.service.ProductService;

import com.oracle.mall.service.NavService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class ListController {

    @Autowired
    private ProductService productService;

    @Autowired
    private NavService navService;
    @Autowired
    private CartService cartService;

    @RequestMapping("/list")
    public String list(Model model, Integer cid, Integer bid, String keyWord, @RequestParam(required = false,defaultValue = "1") Integer page, HttpSession session){

        //List<Product> products = productService.query(cid,bid,keyWord,page);
        ProductPage productPage = productService.query(cid,bid,keyWord,page);


        model.addAttribute("productPage",productPage);

        List<Nav> nav = navService.getNav();
        model.addAttribute("nav",nav);


        if(session.getAttribute("member")!=null){
            Member member = (Member) session.getAttribute("member");
            Integer mid = member.getMid();
            CartDetail cartDetail = cartService.queryTotal(mid);
            model.addAttribute("cartDetail",cartDetail);
        }

        return "list";
    }
}
